﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XLua_Ki
{
    public class ResourcesManager_Ki : MonoBehaviour
    {
        private Dictionary<string, AssetBundle> bundles_AB_GAME = null;   //所有的AB资源(根据需要再加载prefab)  <string子AB名字,对应的Bundle>
        public Dictionary<string, AssetBundle> Bundles_AB_GAME
        {
            get
            {
                if (bundles_AB_GAME == null)
                {
                    bundles_AB_GAME = new Dictionary<string, AssetBundle>();
                }
                return bundles_AB_GAME;
            }
        }

        public Dictionary<string, GameObject> prefabs_AB_GAME = null;     //所有的prefabs资源(一次性加载)<string预制体名字,对应的obj>
        public Dictionary<string, GameObject> Prefabs_AB_GAME
        {
            get
            {
                if (prefabs_AB_GAME == null)
                {
                    prefabs_AB_GAME = new Dictionary<string, GameObject>();
                }
                return prefabs_AB_GAME;
            }
        }

        private Dictionary<string, AssetBundle> mainBundels = null;       //记录加载过的MainAB
        public Dictionary<string, AssetBundle> MainBundels
        {
            get
            {
                if (mainBundels == null)
                {
                    mainBundels = new Dictionary<string, AssetBundle>();
                }
                return mainBundels;
            }
        }

        private string abDataPath = null; //局部变量,记录主AB路径.
        private string mainABName = null; //局部变量,记录主AB的名字.

        //加载所有的Prefabs.
        public void LoadALLABPrefabs(string mainABRoot)
        {
            mainABName = Util.GetDirectoryName(mainABRoot);

            if (MainBundels.ContainsKey(mainABName))
            {
                StartCoroutine(OnLoadALLAB(abDataPath));
            }
            else
            {
                abDataPath = Util.GetBackslashForDirPath(mainABRoot);
                if (Util.ExistsFile(abDataPath + "Lua/", true))
                {
                    Util.DOLuaFile(abDataPath + "Lua/");
                }
                else
                {
                    Debug.Log(string.Format("can't find {0} ", abDataPath + "Lua/"));
                }

                if (Util.ExistsFile(abDataPath + mainABName, false))
                {
                    StartCoroutine(OnLoadALLAB(abDataPath));
                }
            }
        }

        //加载所有的Bundles.
        public void LoadALLABbundles(string mainABRoot)
        {
            mainABName = Util.GetDirectoryName(mainABRoot);

            abDataPath = Util.GetBackslashForDirPath(mainABRoot);
            if (MainBundels.ContainsKey(mainABName) == false)
            {
                if (Util.ExistsFile(abDataPath + "Lua/", true))
                {
                    Util.DOLuaFile(abDataPath + "Lua/");
                }
                else
                {
                    Debug.Log(string.Format("can't find {0} ", abDataPath + "Lua/"));
                }
            }
            if (Util.ExistsFile(abDataPath + mainABName, false))
            {
                StartCoroutine(OnLoadALLAB(abDataPath, false));
            }
        }

        // 开始加载AB资源(异步加载).
        IEnumerator OnLoadALLAB(string abDataPath, bool isLoadPrefabs = true)
        {
            var mainABRequest = AssetBundle.LoadFromFileAsync(abDataPath + mainABName);
            yield return mainABRequest;
            if (mainABRequest.isDone == true)
            {
                var MainAB = mainABRequest.assetBundle;
                if (MainAB == null)
                {
                    Debug.Log("fail to load MainAB.");
                    yield break;
                }

                if (mainBundels.ContainsKey(mainABName))
                {
                    mainBundels[mainABName] = MainAB;
                }
                else
                {
                    mainBundels.Add(mainABName, MainAB);
                }

                AssetBundleManifest manifest = MainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
                string[] ABnames = manifest.GetAllAssetBundles();

                foreach (var item in ABnames)
                {
                    LoadChildABAndPrefabs(abDataPath + item, item, isLoadPrefabs);
                }
            }
        }

        //加载所有ChildAB和Prefabs资源(同步加载).
        private void LoadChildABAndPrefabs(string childABpath, string childABName, bool isLoadPrefabs)
        {
            var childAB = AssetBundle.LoadFromFile(childABpath);
            var prefabNames = childAB.GetAllAssetNames();

            Bundles_AB_GAME.Add(childABName, childAB);

            if (isLoadPrefabs == false) return;
            foreach (var item in prefabNames)
            {
                string prefab_Name = Util.GetFileNameWithoutExtension(item);
                var obj = childAB.LoadAsset<GameObject>(prefab_Name);

                GameObject m_obj = null;
                if (Prefabs_AB_GAME.TryGetValue(prefab_Name, out m_obj))
                {
                    if (m_obj == null) Prefabs_AB_GAME[prefab_Name] = obj;
                }
                else
                {
                    Prefabs_AB_GAME.Add(prefab_Name, obj);
                }
            }
        }
    }
}
